﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LinkCard.Model;
using LinkCard.Dao;
//using LinkCard.AdminCP.Common;
using System.Data;
using LinkCard.Utils;
using LinkCard.Model;

namespace LinkCard.AdminCP.MktSupplier
{
    public partial class supplier_edit : System.Web.UI.Page
    {
        protected bool nInTemp;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["USER"] == null)
            {
                Response.Redirect("/login.aspx");
            }


            if (!IsPostBack)
            {
                Int64 nSupplierID = ConvertUtility.ToInt64(Request.QueryString["SupplierID"]);
                SupplierID.Value = nSupplierID.ToString();
                if (nSupplierID > 0)
                {
                    //btnUpdate.Visible = false;
                    //btnPublish.Visible = false;
                    //btnDel.Visible = false;
                    //btnAccept.Visible = false;
                    //btnDrop.Visible = false;

                    string sSQL = "";
                    //nInTemp = inTemp(nSupplierID);
                    //if (nInTemp)
                    //{
                    //    sSQL = "SELECT * FROM SupplierTemp WHERE SupplierID =" + nSupplierID.ToString() + "";
                    //    btnAccept.Visible = true;
                    //    //btnDrop.Visible = true;

                    //}
                    //else
                    //{
                        sSQL = "SELECT * FROM Supplier WHERE SupplierID =" + nSupplierID.ToString() + "";
                    //    //btnPublish.Visible = true;
                    //    //btnDel.Visible = true;

                    //}

                    DataTable dtSupplier = LinkCard.Utils.DataHelper.SelectTable(sSQL, false);
                    if (dtSupplier.Rows.Count > 0)
                    {

                        
                        Name.Text = dtSupplier.Rows[0]["Name"].ToString();
                        Director.Text = dtSupplier.Rows[0]["Director"].ToString();
                        Address.Text = dtSupplier.Rows[0]["Address"].ToString();
                        Phone.Text = dtSupplier.Rows[0]["Phone"].ToString();
                        Fax.Text = dtSupplier.Rows[0]["Fax"].ToString();
                        Email.Text = dtSupplier.Rows[0]["Email"].ToString();
                        TaxCode.Text = dtSupplier.Rows[0]["TaxCode"].ToString();
                        ContactPersonName.Text = dtSupplier.Rows[0]["ContactPersonName"].ToString();
                        ContactPersonMail.Text = dtSupplier.Rows[0]["ContactPersonMail"].ToString();
                        Business.Text = dtSupplier.Rows[0]["Business"].ToString();
                        AuthorizationStatement.Value = dtSupplier.Rows[0]["AuthorizationStatement"].ToString();
                        ContractNumber.Text = dtSupplier.Rows[0]["ContractNumber"].ToString();
                        ContractStatement.Value = dtSupplier.Rows[0]["ContractStatement"].ToString();
                        //DiscountPercent.Text = dtSupplier.Rows[0]["DiscountPercent"].ToString();
                        //PricePercent.Text = dtSupplier.Rows[0]["PricePercent"].ToString();
                        //PointPercent.Text = dtSupplier.Rows[0]["PointPercent"].ToString();

                        //DiamondDiscountPercent.Text = dtSupplier.Rows[0]["DiamondDiscountPercent"].ToString();
                        //DiamondPricePercent.Text = dtSupplier.Rows[0]["DiamondPricePercent"].ToString();
                        //DiamondPointPercent.Text = dtSupplier.Rows[0]["DiamondPointPercent"].ToString();

                        //PlatinumDiscountPercent.Text = dtSupplier.Rows[0]["PlatinumDiscountPercent"].ToString();
                        //PlatinumPricePercent.Text = dtSupplier.Rows[0]["PlatinumPricePercent"].ToString();
                        //PlatinumPointPercent.Text = dtSupplier.Rows[0]["PlatinumPointPercent"].ToString();

                        //Password.Value = dtSupplier.Rows[0]["Password"].ToString();
                        //Status.Value = dtSupplier.Rows[0]["Status"].ToString();
                        //if (ConvertUtility.ToByte(dtSupplier.Rows[0]["Status"].ToString()) == 0) btnUpdate.Visible = true;

                        WebsiteAdd.Text = dtSupplier.Rows[0]["Website"].ToString();
                        Logo.Value = dtSupplier.Rows[0]["Logo"].ToString();
                        LogoHome.Value = dtSupplier.Rows[0]["LogoHome"].ToString();
                        Introduction.Value = dtSupplier.Rows[0]["Introduction"].ToString();
                        //Username.Text = dtSupplier.Rows[0]["Username"].ToString();
                        fckGoogleMap.Value = dtSupplier.Rows[0]["Gmap"].ToString();
                        //txtTags.Value = dtSupplier.Rows[0]["Tags"].ToString();
                    }


                }
            }

        }

        protected bool inTemp(Int64 nSupplierID)
        {

            DataTable dtSupplierTemp = LinkCard.Utils.DataHelper.SelectTable("SELECT SupplierID FROM SupplierTemp WHERE SupplierID =" + nSupplierID.ToString() + "", false);
            if (dtSupplierTemp.Rows.Count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        protected Int64 getUserID()
        {

            DataTable dt = LinkCard.Utils.DataHelper.SelectTable("SELECT UserID FROM [User] WHERE Username ='" + Session["USER"].ToString() + "'", false);
            if (dt.Rows.Count > 0)
            {
                return ConvertUtility.ToInt64(dt.Rows[0]["UserID"]);
            }
            else
            {
                return 0;
            }
        }

    
    

        protected void btnAccept_Click(object sender, EventArgs e)
        {
            updateSupplier(2);
            //Lưu log
            var logAction = LogActions.LogItem(getUserID(), Convert.ToInt64(this.SupplierID.Value), 0, 0, 0, "Đồng ý Đối tác", "PublishedSupplier", "AdminCP - Supplier");

            // Xóa Temp
            //string sSQL = "delete SupplierTemp where SupplierID = " + SupplierID.Value;
            //LinkCard.Utils.DataHelper.ExecuteNonQuery(sSQL, true);

            Response.Redirect("/MktSupplier/supplier_all_published.aspx");
        }


        protected void updateSupplier(byte status)
        {
            var db = new TopLinkEntities();
            //var dao = new SupplierDao();
            Int64 nSupplierID = ConvertUtility.ToInt64(SupplierID.Value);

            var _supplier = db.Supplier.Where(t => t.SupplierID == nSupplierID).FirstOrDefault();

            _supplier.Name = Name.Text;
            _supplier.Director = Director.Text;
            _supplier.Address = Address.Text;
            _supplier.Phone = Phone.Text;
            _supplier.Fax = Fax.Text;
            _supplier.Email = Email.Text;
            _supplier.TaxCode = TaxCode.Text;
            _supplier.ContactPersonName = ContactPersonName.Text;
            _supplier.ContactPersonMail = ContactPersonMail.Text;
            _supplier.Business = Business.Text;
            _supplier.AuthorizationStatement = AuthorizationStatement.Value;
            _supplier.ContractNumber = ContractNumber.Text;
            _supplier.ContractStatement = ContractStatement.Value;
            _supplier.Status = status;


            //if (PasswordNew.Text.Trim() == "")
            //{
            //    _supplier.Password = Password.Value;
            //}
            //else
            //{
            //    _supplier.Password = PasswordNew.Text.Trim();
            //}
            //_supplier.DiscountPercent = ConvertUtility.ToDouble(DiscountPercent.Text);
            //_supplier.PointPercent = ConvertUtility.ToDouble(PointPercent.Text);
            //_supplier.PricePercent = ConvertUtility.ToDouble(PricePercent.Text);

            //_supplier.DiamondDiscountPercent = ConvertUtility.ToDouble(DiamondDiscountPercent.Text);
            //_supplier.DiamondPointPercent = ConvertUtility.ToDouble(DiamondPointPercent.Text);
            //_supplier.DiamondPricePercent = ConvertUtility.ToDouble(DiamondPricePercent.Text);

            //_supplier.PlatinumDiscountPercent = ConvertUtility.ToDouble(PlatinumDiscountPercent.Text);
            //_supplier.PlatinumPointPercent = ConvertUtility.ToDouble(PlatinumPointPercent.Text);
            //_supplier.PlatinumPricePercent = ConvertUtility.ToDouble(PlatinumPricePercent.Text);

            _supplier.Introduction = Introduction.Value;
            _supplier.Logo = Logo.Value;
            _supplier.LogoHome = LogoHome.Value;
            _supplier.Website = WebsiteAdd.Text;
//            _supplier.Username = Username.Text;
            _supplier.Gmap = fckGoogleMap.Value;
            //_supplier.Tags = txtTags.Value;
            db.SaveChanges();

            if (!string.IsNullOrEmpty(_supplier.Tags))
            {
                var addTag = new TagDao().AddSupplierTag(nSupplierID, _supplier.Tags);
            }

        }
    }
}